package com.js.lib_common.base;


import com.js.lib_common.constant.AppConstant;
import com.js.lib_common.http.ApiClient;
import com.js.lib_common.http.ApiStores;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 *
 */
public class BasePresenter<V> {
    public V mvpView;
    protected ApiStores apiStores;
    private CompositeDisposable mCompositeDisposable;

    public void attachView(V mvpView) {
        this.mvpView = mvpView;
        apiStores = ApiClient.retrofit(AppConstant.API_SCADA_URL[AppConstant.urlType]).create(ApiStores.class);

    }


    public void detachView() {
        this.mvpView = null;
        onUnSubscribe();
    }


    //RxJava取消注册，以避免内存泄露
    public void onUnSubscribe() {
        if (mCompositeDisposable != null) {
            mCompositeDisposable.dispose();
        }
    }


    public void addSubscription(Observable observable, DisposableObserver observer) {
        if (mCompositeDisposable == null) {
            mCompositeDisposable = new CompositeDisposable();
        }

        mCompositeDisposable.add(observer);
        observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(observer);
    }


    /**
     * 对请求头设置
     * Created by wz on 2019/5/17.
     *//*
    public static class HeaderInterceptor implements Interceptor {
        public int maxRetry;//最大重试次数
        private int retryNum = 2;//假如设置为3次重试的话，则最大可能请求4次（默认1次+3次重试）

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request originalRequest = chain.request();
            String header = originalRequest.header("Authorization");
            Request.Builder requestBuilder = originalRequest.newBuilder()
                    .method(originalRequest.method(), originalRequest.body());
            if (header == null) {
                requestBuilder.header("Authorization", getToken());
            }
            Request request = requestBuilder.build();
            Response response = chain.proceed(request);
            while (!response.isSuccessful() && retryNum < maxRetry) {
                retryNum++;
                System.out.println("retryNum=" + retryNum);
                response = chain.proceed(request);
                Log.e("retryNum", "retryNum=" + retryNum);
            }
            return response;


        }

        public String getToken() {
            ACache acache = ACache.get(LibCommon.mContext);
            LoginResponse bean = (LoginResponse) acache.getAsObject("LoginResponse");
            return bean.getToken_type() + " " + bean.getAccess_token();
        }

    }*/


}
